<template>
  <div class="home">
    <h1>Admin Home</h1>
    <div class="after-login">
      {{loginUser}}
      <button @click="exit">Exit</button>
    </div>

    <router-link :to="{name: 'Goods'}">goto goods manager</router-link>
  </div>
</template>

<script lang="ts" setup>
 import {UserService} from '@/service'
 import { onMounted, ref } from '@vue/runtime-core';
 import { useRoute, useRouter } from 'vue-router';
 const router = useRouter()
 const route = useRoute()
 const loginUser = ref('')

 async function exit() {
   let {message, data} = await UserService.exit()
   if(!data) {
     alert(message)
   } else {
     loginUser.value = ''
   }

   router.push('/')
 }

 onMounted(async () => {
   let {data} = await UserService.getLoginUser()
   if(data == null || data != 'admin') {
     router.push({name: 'Home'})
     return
   }

   loginUser.value = route.params['loginUser'] as string
 })
</script>